<?php

/*
	Class: Communitas_Integrate_CustomerController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Communitas_Integrate_CustomerController extends Mage_Core_Controller_Front_Action {

	/* Group: Actions */

	/*
		Function: login
			This is a redirect so that when you try to log into the Magento site,
			you are redirected to the Communitas site login page.
			This redirect is set up as a rewrite in
			/app/code/local/Communitas/Integrate/etc/config.xml
	*/
	public function loginAction() {
		if (Mage::getSingleton('customer/session')->isLoggedIn()) {
			$url = Mage::getUrl('customer/account');
		} else {
			$vars = parse_ini_file($_SERVER['DOCUMENT_ROOT']."/app/code/local/Communitas/Integrate/etc/variables.ini", true);
			$url = $vars['communitas_login_url'];
		}
		$this->getResponse()->setRedirect($url);
	}

	/*
		Function: logout
			This is a redirect so that when you log out of Magento using its logout URL,
			you are then redirected back to the Communitas site.
			From the Communitas site, you can link to http://yourmagentosite.com/customer/account/logout
			and you will be logged out of the Magento site and sent back to the Communitas site
			in less than the blink of an eye.
			This is very useful when you do not ever want customers to hit the Magento installation directly.
			This redirect is set up as a rewrite in
			/app/code/local/Communitas/Integrate/etc/config.xml
	*/
	public function logoutAction() {
		if (Mage::getSingleton('customer/session')->isLoggedIn()) {
			Mage::getSingleton('customer/session')->logout();
		}
		$vars = parse_ini_file($_SERVER['DOCUMENT_ROOT']."/app/code/local/Communitas/Integrate/etc/variables.ini", true);
		$this->getResponse()->setRedirect($vars['communitas_homepage_url']);
	}

	/*
		Function: loginbounce
			The purpose of this "bounce" is so that a customer can get a Magento browser cookie.
			If a Communitas user does not have a Magento "frontend" cookie, they are sent here to get one,
			then immediately sent back to the Communitas app.

			There is no cookie code in this function - Magento sets up the cookie elsewhere.
			Just hitting any frontend action in Magento causes the frontend cookie to be set.
	*/
	public function loginbounceAction() {
		$request = $this->getRequest();
		$backto = base64_decode($request->getParam('backto', null));
		if (!is_null($backto)) {
			$this->getResponse()->setRedirect($backto);
		} else {
			$vars = parse_ini_file($_SERVER['DOCUMENT_ROOT']."/app/code/local/Communitas/Integrate/etc/variables.ini", true);
			$this->getResponse()->setRedirect($vars['communitas_homepage_url']);
		}
	}

}
